﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace YWTSSys.Library
{
    /*
     *  序列化示例
            PluginItemCollection c = new PluginItemCollection();
            PluginItem item = new PluginItem();
            item.PluginID = new Guid();
            item.Title = "p1";
            item.PluginName = "插件3";
            item.Enable = true;
            item.Description = "我试下";
            item.AssemblyPath = "d:\\ss.dll";

            c.Add(item);
            XmlSerializerHelper.SerializeToFile(AppDomain.CurrentDomain.BaseDirectory + "/Plugin.config", c);


            //反序列化示例
            PluginItemCollection p = XmlSerializerHelper.DeserializeFromFile<PluginItemCollection>(AppDomain.CurrentDomain.BaseDirectory + "/Plugin.config");
            foreach (var item in p)
            {
                MessageBox.Show(item.PluginName);
            }
     */
    /// <summary>
    /// XML序列化
    /// </summary>
    public class XmlSerializerHelper
    {
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="propertys"></param>
        /// <returns></returns>
        public static string Serialize(object propertys)
        {
            XmlWriterSettings settings;
            StringBuilder container = new StringBuilder();//创建字符串
            //string container=AppDomain.CurrentDomain.BaseDirectory+"/Plugin.config";


            settings = CreateWriterSettings();
            settings.ConformanceLevel = ConformanceLevel.Auto;

            using (XmlWriter xmlWriter = XmlWriter.Create(container, settings))
            {
                XmlSerializer seri = new XmlSerializer(propertys.GetType());
                seri.Serialize(xmlWriter, propertys);
                xmlWriter.Flush();
            }

            return container.ToString();
        }
        /// <summary>
        /// 序列化到指定文件
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool SerializeToFile(string xmlPath, object obj)
        {
            if (!File.Exists(xmlPath))
            {
                File.Create(xmlPath);
            }

            string serStr = Serialize(obj);
            try
            {
                File.WriteAllText(xmlPath, serStr, Encoding.UTF8);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 从字符串中反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xmlStr"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string xmlStr) where T : new()
        {
            try
            {
                XmlReaderSettings settings = CreateReaderSettings();
                T _obj;
                Type type = typeof(T);

                using (XmlReader reader = XmlReader.Create(new System.IO.StringReader(xmlStr), settings))
                {
                    XmlSerializer seri = new XmlSerializer(type);

                    _obj = (T)seri.Deserialize(reader);

                    return _obj;
                }
            }
            catch
            {
                return new T();
            }
        }
        /// <summary>
        /// 从文件中反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xmlPath"></param>
        /// <returns></returns>
        public static T DeserializeFromFile<T>(string xmlPath) where T : new()
        {
            if (!File.Exists(xmlPath))
                return new T();

            string content = "";
            using (StreamReader sr = File.OpenText(xmlPath))
            {
                content = sr.ReadToEnd();

            }
            if (string.IsNullOrEmpty(content))
                return new T();

            return Deserialize<T>(content);
        }


        private static XmlReaderSettings CreateReaderSettings()
        {
            XmlReaderSettings settings = new XmlReaderSettings();

            settings.CloseInput = true;
            settings.IgnoreComments = true;
            settings.IgnoreWhitespace = true;

            return settings;
        }
        private static XmlWriterSettings CreateWriterSettings()
        {
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Indent = false;
            //settings.IndentChars = ("    ");
            settings.Encoding = System.Text.Encoding.Unicode;

            return settings;
        }
    }
}
